home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / winterp-1.13 / examples / xlisp-1.6 / art.lsp next >
Encoding:
Lisp/Scheme  |  1991-10-06  |  3.5 KB  |  89 lines

  1. ; -*-Lisp-*-
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;
  4. ; File:         art.lsp
  5. ; RCS:          $Header: $
  6. ; Description:  This is an example using the object-oriented programming support in
  7. ;        XLISP.  The example involves defining a class of objects representing
  8. ;        dictionaries.  Each instance of this class will be a dictionary in
  9. ;        which names and values can be stored.  There will also be a facility
  10. ;        for finding the values associated with names after they have been
  11. ;        stored.
  12. ; Author:       ???
  13. ; Created:      Sat Oct  5 20:47:56 1991
  14. ; Modified:     Sat Oct  5 20:48:55 1991 (Niels Mayer) mayer@hplnpm
  15. ; Language:     Lisp
  16. ; Package:      N/A
  17. ; Status:       X11r5 contrib tape release
  18. ;
  19. ; WINTERP Copyright 1989, 1990, 1991 Hewlett-Packard Company (by Niels Mayer).
  20. ; XLISP version 2.1, Copyright (c) 1989, by David Betz.
  21. ;
  22. ; Permission to use, copy, modify, distribute, and sell this software and its
  23. ; documentation for any purpose is hereby granted without fee, provided that
  24. ; the above copyright notice appear in all copies and that both that
  25. ; copyright notice and this permission notice appear in supporting
  26. ; documentation, and that the name of Hewlett-Packard and Niels Mayer not be
  27. ; used in advertising or publicity pertaining to distribution of the software
  28. ; without specific, written prior permission.  Hewlett-Packard and Niels Mayer
  29. ; makes no representations about the suitability of this software for any
  30. ; purpose.  It is provided "as is" without express or implied warranty.
  31. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  32.  
  33. ; Create the 'Dictionary' class and establish its instance variable list.
  34. ; The variable 'entries' will point to an association list representing the
  35. ; entries in the dictionary instance.
  36.  
  37. (setq Dictionary (Class :new '(entries)))
  38.  
  39. ; Setup the method for the ':isnew' initialization message.
  40. ; This message will be send whenever a new instance of the 'Dictionary'
  41. ; class is created.  Its purpose is to allow the new instance to be
  42. ; initialized before any other messages are sent to it.  It sets the value
  43. ; of 'entries' to nil to indicate that the dictionary is empty.
  44.  
  45. (Dictionary :answer :isnew '()
  46.         '((setq entries nil)
  47.           self))
  48.  
  49. ; Define the message ':add' to make a new entry in the dictionary.  This
  50. ; message takes two arguments.  The argument 'name' specifies the name
  51. ; of the new entry; the argument 'value' specifies the value to be
  52. ; associated with that name.
  53.  
  54. (Dictionary :answer :add '(name value)
  55.         '((setq entries
  56.                 (cons (cons name value) entries))
  57.           value))
  58.  
  59. ; Create an instance of the 'Dictionary' class.  This instance is an empty
  60. ; dictionary to which words may be added.
  61.  
  62. (setq d (Dictionary :new))
  63.  
  64. ; Add some entries to the new dictionary.
  65.  
  66. (d :add 'mozart 'composer)
  67. (d :add 'winston 'computer-scientist)
  68.  
  69. ; Define a message to find entries in a dictionary.  This message takes
  70. ; one argument 'name' which specifies the name of the entry for which to
  71. ; search.  It returns the value associated with the entry if one is
  72. ; present in the dictionary.  Otherwise, it returns nil.
  73.  
  74. (Dictionary :answer :find '(name &aux entry)
  75.         '((cond ((setq entry (assoc name entries))
  76.           (cdr entry))
  77.          (t
  78.           nil))))
  79.  
  80. ; Try to find some entries in the dictionary we created.
  81.  
  82. (d :find 'mozart)
  83. (d :find 'winston)
  84. (d :find 'bozo)
  85.  
  86. ; The names 'mozart' and 'winston' are found in the dictionary so their
  87. ; values 'composer' and 'computer-scientist' are returned.  The name 'bozo'
  88. ; is not found so nil is returned in this case.
  89.